package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.po.dto.WalletInfoDTO;
import com.neusoft.elmboot.service.WalletService;
import com.neusoft.elmboot.util.response.ResultBody;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/elm/wallet")
public class WalletController {
    @Resource
    private WalletService walletService;

    @PostMapping("/init")
    public ResultBody initWallet(@RequestParam String walletId,
                                 @RequestParam String payPwd) throws Exception{
        walletService.init(walletId, payPwd);
        return ResultBody.success();
    }

    @PostMapping("/setVIP")
    public ResultBody setVIP(@RequestParam List<String> walletIdList) throws Exception{
        walletService.setVIP(walletIdList);
        return ResultBody.success();
    }

    @PostMapping("/setOverdraftAmount")
    public ResultBody setOverdraftAmount(@RequestParam String walletId,
                                         @RequestParam BigDecimal amount) throws Exception{
        walletService.setOverdraftAmount(walletId, amount);
        return ResultBody.success();
    }

    @PostMapping("/info")
    public ResultBody getWalletInfo(@RequestParam String walletId) throws Exception{
        // 使用DTO格式返回，确保数据类型和格式正确
        WalletInfoDTO walletInfo = walletService.getWalletInfo(walletId);
        return ResultBody.success(walletInfo);
    }

    @PostMapping("/transInfo")
    public ResultBody getTransInfo(String walletId, Integer index) throws Exception{
        return ResultBody.success(walletService.getTransInfo(walletId, index));
    }

    @PostMapping("/recharge")
    public ResultBody recharge(@RequestParam String walletId,
                               @RequestParam BigDecimal amount) throws Exception {
        try {
            walletService.recharge(walletId, amount);
            return ResultBody.success("充值成功");
        } catch (Exception e) {
            // 异常会被GlobalExceptionHandler捕获并处理
            throw e;
        }
    }

    @PostMapping("/repay")
    public ResultBody repay(@RequestParam String walletId,
                            @RequestParam BigDecimal amount) throws Exception {
        walletService.repay(walletId, amount);
        return ResultBody.success();
    }

    @PostMapping("/borrow")
    public ResultBody borrow(@RequestParam String walletId,
                             @RequestParam BigDecimal amount) throws Exception {
        walletService.borrow(walletId, amount);
        return ResultBody.success("借贷成功");
    }

    @PostMapping("/withdraw")
    public ResultBody withdraw(@RequestParam String walletId,
                               @RequestParam String payPwd,
                               @RequestParam BigDecimal amount) throws Exception {
        walletService.withdraw(walletId, payPwd, amount);
        return ResultBody.success();
    }

    @PostMapping("/pay")
    public ResultBody pay(@RequestParam String fromWalletId,
                          @RequestParam String toWalletId,
                          @RequestParam String payPwd,
                          @RequestParam BigDecimal amount) throws Exception {
        String transId = walletService.pay(fromWalletId, toWalletId, payPwd, amount);
        return ResultBody.success(transId);
    }

    @PostMapping("/confirm")
    public ResultBody confirm(@RequestParam String transId) throws Exception {
        walletService.confirm(transId);
        return ResultBody.success();
    }

}
